package com.os.opencv.java.chapter8;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.LinkedList;
import java.util.List;

public class CompareHist {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/Pictures/tiger.jpg", Imgcodecs.IMREAD_GRAYSCALE);
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //对图像进行中值滤波并在屏幕上显示
        Mat src2 = new Mat();
        Imgproc.medianBlur(src, src2, 5);
        HighGui.imshow("median blur", src2);
        HighGui.waitKey(0);

        //直方图的参数设置
        float[] range = {0, 256};
        MatOfFloat histRange = new MatOfFloat(range);

        //图像1的直方图数据统计归一化
        Mat hist = new Mat();
        List<Mat> matList = new LinkedList<>();
        matList.add(src);
        Imgproc.calcHist(matList, new MatOfInt(0), new Mat(), hist, new MatOfInt(256), histRange);
        Core.normalize(hist, hist, 0, 400, Core.NORM_MINMAX);

        //图像2的直方图数据统计并归一化
        Mat hist2 = new Mat();
        List<Mat> matList2 = new LinkedList<>();
        matList2.add(src2);
        Imgproc.calcHist(matList2, new MatOfInt(0), new Mat(), hist2, new MatOfInt(256), histRange);
        Core.normalize(hist2, hist2, 0, 400, Core.NORM_MINMAX);
        double s = Imgproc.compareHist(hist, hist2, Imgproc.HISTCMP_CORREL);

        System.out.println("相似度=" + s);
        System.exit(0);


    }
}
